// Learn cc.Class:
//  - [Chinese] http://docs.cocos.com/creator/manual/zh/scripting/class.html
//  - [English] http://www.cocos2d-x.org/docs/creator/en/scripting/class.html
// Learn Attribute:
//  - [Chinese] http://docs.cocos.com/creator/manual/zh/scripting/reference/attributes.html
//  - [English] http://www.cocos2d-x.org/docs/creator/en/scripting/reference/attributes.html
// Learn life-cycle callbacks:
//  - [Chinese] http://docs.cocos.com/creator/manual/zh/scripting/life-cycle-callbacks.html
//  - [English] http://www.cocos2d-x.org/docs/creator/en/scripting/life-cycle-callbacks.html

cc.Class({
    extends: cc.Component,

    properties: {
        bqjBtn:cc.Button,
        quanBtn:cc.Button,
        bqjLayout:cc.Layout,
        quanLayout:cc.Layout,
    },

    onLoad () {
        if(cc.sys.localStorage.getItem("firstGame") == null){
            cc.sys.localStorage.setItem("getBqj","1");
            cc.sys.localStorage.setItem("getQq","1");
            cc.sys.localStorage.setItem("curBqjSelect","1");
            cc.sys.localStorage.setItem("curQqSelect","1");
            cc.sys.localStorage.setItem("firstGame","1");
        }

        this.showSkin();
    },

    start () {

    },

    bqyClicked(event){
        this.bqjBtn.interactable = false;
        this.quanBtn.interactable = true;
        this.bqjLayout.node.active = true;
        this.quanLayout.node.active = false;

    },
    quanClick(event){
        this.bqjBtn.interactable = true;
        this.quanBtn.interactable = false;
        this.quanLayout.node.active = true;
        this.bqjLayout.node.active = false;
    },

    getBqjClick(event){
        var curIndex =  event.target._name.charAt(event.target._name.length - 1);

        var select = cc.find("select",event.target);
        for (var i = 1; i < 7; i++){
            event.target.parent.getChildByName('bq' + i).getChildByName('select').active = false;
        }    

        select.active = true;

        if(event.target.getChildByName('lock_layout').active == false){
            event.target.parent.getChildByName('get_qun').active = false;
            cc.sys.localStorage.setItem("curBqjSelect",curIndex);
        }else{
            event.target.parent.getChildByName('get_qun').active = true;
        }
    },

    getQuanClick(event){
        var curIndex =  event.target._name.charAt(event.target._name.length - 1);

        var select = cc.find("select",event.target);
        for (var i = 1; i < 7; i++){
            event.target.parent.getChildByName('qq' + i).getChildByName('select').active = false;
        }    

        select.active = true;

        if(event.target.getChildByName('lock_layout').active == false){
            event.target.parent.getChildByName('get_qq').active = false;
            cc.sys.localStorage.setItem("curQqSelect",curIndex);
        }else{
            event.target.parent.getChildByName('get_qq').active = true;
        }
    },

    unlockBqjClick(event){
        //获取哪个被选中
        for(var i = 1; i < 7;i++){
            if(event.target.parent.parent.getChildByName('bq' + i).getChildByName('select').active == true){
                event.target.parent.parent.getChildByName('bq' + i).getChildByName('lock_layout').active = false;
                var getBqj = cc.sys.localStorage.getItem("getBqj");
                if(getBqj.indexOf(i)== -1){
                    cc.sys.localStorage.setItem("getBqj",getBqj + i);
                    cc.sys.localStorage.setItem("curBqjSelect",i);
                }
                
            }
        }
    },

    unlockQuanClick(event){
        for(var i = 1; i < 7;i++){
            if(event.target.parent.parent.getChildByName('qq' + i).getChildByName('select').active == true){
                event.target.parent.parent.getChildByName('qq' + i).getChildByName('lock_layout').active = false;
                var getQq = cc.sys.localStorage.getItem("getQq");
                if(getQq.indexOf(i) == -1){
                    cc.sys.localStorage.setItem("getQq",getQq + i);
                    cc.sys.localStorage.setItem("curQqSelect",i);
                }

                
            }
        }

    },

    showSkin(){
       var getBqj =  cc.sys.localStorage.getItem("getBqj");
       var getQq =  cc.sys.localStorage.getItem("getQq");
       for(var i = 0; i < getBqj.length;i++){
            var lock_layout = cc.find("bqd_layout/bq" + getBqj[i] + "/lock_layout",this.node);
            lock_layout.active = false;
       }

       for(var i = 0; i < getQq.length;i++){
            var lock_layout = cc.find("qq_layout/qq" + getQq[i] + "/lock_layout",this.node);
            lock_layout.active = false;
        }

        var indexBqj =  cc.sys.localStorage.getItem("curBqjSelect");
        var indexQq =  cc.sys.localStorage.getItem("curQqSelect");
        //设置选中的状态
        var select = cc.find("bqd_layout/bq" + indexBqj + "/select",this.node);
        select.active = true;
        cc.find("bqd_layout/get_qun",this.node).active = false;

        var select = cc.find("qq_layout/qq" + indexQq + "/select",this.node);
        select.active = true;
        cc.find("qq_layout/get_qq",this.node).active = false;

    }

    // update (dt) {},
});
